package com.einmeer.hfbank.service.impl;

import com.einmeer.hfbank.entity.Card;
import com.einmeer.hfbank.entity.Deposit;
import com.einmeer.hfbank.entity.InterestRate;
import com.einmeer.hfbank.hfconst.CardConst;
import com.einmeer.hfbank.mapper.CardMapper;
import com.einmeer.hfbank.mapper.DepositMapper;
import com.einmeer.hfbank.mapper.InterestRateMapper;
import com.einmeer.hfbank.mapper.UserMapper;
import com.einmeer.hfbank.service.CardService;
import com.einmeer.hfbank.vo.ResultVo;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.sql.Timestamp;
import java.util.List;

/**
 * @author 芊嵛
 * @date 2024/1/12
 */

@Service
@Transactional
public class CardServiceImpl implements CardService {
    @Resource
    private UserMapper userMapper;
    @Resource
    private CardMapper cardMapper;
    @Resource
    private DepositMapper depositMapper;
    @Resource
    private InterestRateMapper interestRateMapper;

    /**
     * 银行卡绑定
     *
     * @param userId
     * @param cardNumber
     * @param userIDCard
     * @param cardPassword
     * @return
     */
    @Override
    public ResultVo Binding(Long userId, Long cardNumber, String userIDCard, Integer cardPassword) {
        Integer n1 = cardMapper.selectBinding(userId, cardNumber);
        if (n1 != null) {
            return new ResultVo(CardConst.CARDNO, CardConst.EXIST);
        }
        Integer n2 = cardMapper.updateBinding(userId, cardNumber, userIDCard, cardPassword);
        if (n2 == 0) {
            return new ResultVo(CardConst.CARDNO, CardConst.UPDATENO);
        }
        return new ResultVo(CardConst.CARDOK, CardConst.UPDATEOK);
    }

    /**
     * 查询用户所有银行卡活期信息
     *
     * @param userId
     * @return
     */
    @Override
    public ResultVo queryAllCards(Long userId) {
        List<Card> list = cardMapper.selectAllCards(userId);
        // 遍历集合是否为空
        if (list.isEmpty()) {
            return new ResultVo(CardConst.CARDNO, CardConst.SELECTNO);
        }
        for (Card temp : list) {
            // 根据卡号获取所有活期记录
            List<Deposit> depositList = depositMapper.selectAllByCardNumber(temp.getCardNumber(), 0);
            // 求这张银行卡活期总金额
            BigDecimal a = new BigDecimal(0);
            String sum = null;
            // 遍历是否存数据了
            if (depositList.isEmpty()) {
                temp.setCardCurrent("0");
            } else {
                InterestRate interestRate = interestRateMapper.selectByType(0);
                // 获取活期利率
                BigDecimal rate = new BigDecimal(interestRate.getIRPercentage());
                // 获取几个月经行更新
                int month = interestRate.getIRMonth();
                // 遍历活期表进行高精度计算
                for (Deposit deposit : depositList) {
                    // 获得时间
                    Timestamp timestamp = deposit.getDepositDate();
                    long oldTime = timestamp.getTime();
                    Timestamp now = new Timestamp(System.currentTimeMillis());
                    long nowTime = now.getTime();
                    // 时间差,相差几个月
                    long afterMonth1 = (nowTime-oldTime) / 1000 / 60 / 60 / 24 / 30;
                    int afterMonth2 = (int) afterMonth1;
                    // 更新过几次
                    int times = deposit.getDepositTimes();
                    // 需要再更新几次
                    int againTimes = afterMonth2 / month;
                    // 获取未更新的depositAmount
                    BigDecimal b = new BigDecimal(deposit.getDepositAmount());
                    // 1+利率
                    BigDecimal c = new BigDecimal(1);
                    rate = rate.add(c);
                    // 得到真实金额
                    int tempAgainTimes = againTimes;
                    while (tempAgainTimes != 0) {
                        b = b.multiply(rate);
                        tempAgainTimes--;
                    }
                    // 更新deposit表,前提数据更新了
                    if (againTimes != 0) {
                        againTimes += times;
                        depositMapper.updateDeposit(deposit.getDepositId(), b.toPlainString(), now, againTimes);
                    }
                    a = a.add(b);
                }
                // 转为字符串
                sum = a.toPlainString();
                temp.setCardCurrent(sum);
                // 把活期存款插入card表中
                cardMapper.updateCardCurrent(temp.getCardNumber(), sum);
            }
        }
        return new ResultVo(CardConst.CARDOK, list, CardConst.SELECTOK);
    }

    /**
     * 显示银行卡详细信息
     *
     * @param cardNumber
     * @return
     */
    @Override
    public ResultVo queryCard(Long cardNumber) {
        Card card = cardMapper.selectCard(cardNumber);
        if (card == null) {
            return new ResultVo(CardConst.CARDNO, CardConst.SELECTNO);
        }
        return new ResultVo(CardConst.CARDOK, card, CardConst.SELECTOK);
    }

    /**
     * 解绑
     *
     * @param cardNumber
     * @param userPassword
     * @return
     */
    @Override
    public ResultVo unbind(Long cardNumber, String userPassword, Long userId) {
        //看密码是否正确
        Long n1 = userMapper.selectUserByIdAndPass(userId, userPassword);
        if (n1 != null) {
            Integer n2 = cardMapper.updateUnbind(cardNumber);
            if (n2 != 0) {
                return new ResultVo(CardConst.CARDOK, CardConst.UPDATEOK);
            }
        }
        return new ResultVo(CardConst.CARDNO, CardConst.UPDATENO);
    }


}
